﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FileSysExp.Core;
using FileSysExp.Models;
using System.Threading;

namespace FileSysExp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MainViewModel _Model;

        public MainWindow()
        {
            InitializeComponent();
            _Model = new MainViewModel();
            _Model.Directories = GetDirectories();
            this.DataContext = _Model;
        }

        private ReadOnlyCollection<DirItemViemModel> GetDirectories()
        {
            List<DirItemViemModel> collection = new List<DirItemViemModel>();
            DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            foreach (DriveInfo di in drives)
            {
                DirItemViemModel dv = new DirItemViemModel();
                dv.Name = di.Name;
                dv.ShortName = di.Name;
                dv.Icon = Win32ApiNativeMethods.GetIcon(di.Name);
                collection.Add(dv);
            }
            return new ReadOnlyCollection<DirItemViemModel>(collection);
        }

        private void FileSystemTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (FileSystemTree.SelectedItem is IFileSysItem)
            {
                DirItemViemModel item = FileSystemTree.SelectedItem as DirItemViemModel;
                item.GetChildren(false);

                IFileSysItem nItem = item.Clone();
                nItem.GetChildren(true);
                _Model.SelectedItem = nItem;
            }
        }

        private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            IFileSysItem item = dataGrid.CurrentItem as IFileSysItem;
            if (item is Models.DirItemViemModel)
            {
                item.GetChildren(true);
                _Model.SelectedItem = item;
            }
        }

    }
}
